package com.example.rocktmqsample.consumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.UtilAll;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(topic = "retry_topic", consumerGroup = "group5")
public class RetryConsumer implements RocketMQListener<String> , RocketMQPushConsumerLifecycleListener {

    @Override
    public void onMessage(String message) {
        // 处理消息的逻辑
        log.info("RetryConsumer Received message: {}",message);
        throw new RuntimeException(message+"重试失败！");
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        // 设置从当前时间开始消费
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_TIMESTAMP);
        consumer.setConsumeTimestamp(UtilAll.timeMillisToHumanString3(System.currentTimeMillis()));

        // 设置最大重试次数
        consumer.setMaxReconsumeTimes(2);

    }
}
